package net.myspring.middle.web;

import com.google.common.collect.Sets;
import net.myspring.common.domain.SearchEntity;
import net.myspring.common.utils.Message;
import net.myspring.common.utils.RequestUtils;
import net.myspring.middle.domain.Role;
import net.myspring.middle.service.MenuService;
import net.myspring.middle.service.PermissionService;
import net.myspring.middle.service.RoleService;
import net.myspring.middle.validator.RoleValidator;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * Created by admin on 2016/8/10.
 */
@Controller
@RequestMapping(value = "sys/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private RoleValidator roleValidator;


    @RequestMapping(value = "list")
    @PreAuthorize("hasAnyAuthority('PERM_sys:role:list:view')")
    public String list(Model model, HttpServletRequest request) {
        SearchEntity searchEntity = RequestUtils.getSearchEntity(request, Role.class.getName());
        model.addAttribute("page", roleService.findPage(searchEntity.getPageable(), searchEntity.getParams()));
        model.addAttribute("s", searchEntity);
        return "sys/roleList";
    }

    @RequestMapping(value = "form")
    @PreAuthorize("hasAnyAuthority('PERM_sys:role:list:edit')")
    public String form(Model model, Role role) {
        model.addAttribute("role", role.getId() == null ? role : roleService.findOne(role.getId()));
        role.setPermissionSet(Sets.newHashSet(permissionService.findByRoleId(role.getId())));
        addAttribute(model, role);
        return "sys/roleForm";
    }

    @RequestMapping(value = "save")
    @PreAuthorize("hasAnyAuthority('PERM_sys:role:list:edit')")
    public String save(Model model, Role role, BindingResult bindingResult, RedirectAttributes redirectAttributes) {
        boolean isCreate = role.isCreate();
        roleValidator.validate(role, bindingResult);
        if (bindingResult.hasErrors()) {
            model.addAttribute("bindingResult", bindingResult);
            addAttribute(model, role);
            return "sys/roleForm";
        }
        roleService.save(role);
        redirectAttributes.addFlashAttribute("message", new Message("用户保存成功"));
        if (isCreate) {
            return "redirect:/sys/role/form";
        } else {
            return "redirect:/sys/role/list";
        }
    }

    @RequestMapping(value = "delete")
    @PreAuthorize("hasAnyAuthority('PERM_sys:role:list:delete')")
    public String delete(Model model, RedirectAttributes redirectAttributes, Role role) {
        roleService.delete(role);
        return "redirect:/sys/role/list";
    }

    @RequestMapping(value = "checkName")
    @ResponseBody
    public boolean checkName(Role role) {
        return roleService.checkName(role);
    }


    @RequestMapping(value = "checkCode")
    @ResponseBody
    public boolean checkCode(Role role) {
        return roleService.checkCode(role);
    }


    private void addAttribute(Model model, Role role) {
        model.addAttribute("permissionTree", permissionService.findPermissionTree(menuService.findTreeMenuList(), role.getPermissionIds()));
    }
}